package com.huifaapp.mall.selectImage;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.RelativeLayout;

import com.huifaapp.mall.R;


/**
 * @author chen
 * popupwindow底部弹出菜单
 */
@SuppressLint({ "InflateParams", "HandlerLeak" })
public abstract class BottomBaseMenu {

    private Context context;
    private RelativeLayout menuLayout;
    private View mView;
    private PopupWindow mPopupWindow;

    public static final int TOP = 1;
    public static final int BOTTOM = 0;
    public int isBottom;
    public void setIsBottom(int isBottom) {
        this.isBottom = isBottom;
    }

    public BottomBaseMenu(Context context) {
        this.context = context;
    }

    public Context getContext(){
        return context;
    }

    private OnParentViewClick mOnParentViewClick;
    public interface OnParentViewClick{
        public void onClick(View view);
    }
    public void setOnParentViewClick(OnParentViewClick listener){
        mOnParentViewClick = listener;
    }

    private OnDismissListener mOnDismissListener;
    public void setOnDismissListener(OnDismissListener listener) {
        this.mOnDismissListener = listener;
    }

    @SuppressWarnings("deprecation")
    public void create() {
        mView = LayoutInflater.from(context).inflate(R.layout.base_bottom_menu_layout, null);
        menuLayout = (RelativeLayout)mView.findViewById(R.id.menuLayout);
        menuLayout.addView(getView());
        menuLayout.setVisibility(View.GONE);

        RelativeLayout.LayoutParams rl = (RelativeLayout.LayoutParams)menuLayout.getLayoutParams();
        if (isBottom == BOTTOM) {
            rl.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        } else {
            rl.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        }

        mPopupWindow = new PopupWindow(mView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        mPopupWindow.setAnimationStyle(R.style.popupwindowStyle);
        mPopupWindow.update();
        mPopupWindow.setTouchable(true);
        mPopupWindow.setFocusable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        mPopupWindow.setOnDismissListener(new OnDismissListener() {
            public void onDismiss() {
                if(mOnDismissListener != null){
                    mOnDismissListener.onDismiss();
                }
            }
        });

        /**
         * mView 的点击事件
         */
        mView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnParentViewClick != null) {
                    mOnParentViewClick.onClick(mView);
                }
                dismiss();
            }
        });
    }

    public View getLayoutView(){
        return menuLayout;
    }

    public PopupWindow getPopupWindow(){
        return mPopupWindow;
    }

    /**
     * 显示菜单
     */
    public void show(){
        mPopupWindow.showAtLocation(mView, Gravity.BOTTOM, 0, 0);
        menuLayout.setVisibility(View.VISIBLE);
        if (getInAnimation() != 0) {
            menuLayout.setAnimation(AnimationUtils.loadAnimation(context, getInAnimation()));
        }
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                mPopupWindow.dismiss();
            }
        }
    };

    /**
     * 关闭菜单
     */
    public void dismiss(){
        menuLayout.setVisibility(View.GONE);
        if (getOutAnimation() != 0) {
            Animation animation = AnimationUtils.loadAnimation(context, getOutAnimation());
            animation.setAnimationListener(new AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    mView.setEnabled(false);
                }
                @Override
                public void onAnimationRepeat(Animation animation) {}
                @Override
                public void onAnimationEnd(Animation animation) {
                    mView.setEnabled(true);
                    handler.sendEmptyMessage(1);
                }
            });
            menuLayout.setAnimation(animation);
        } else {
            mPopupWindow.dismiss();
        }
    }

    protected abstract View getView();

    /**
     * 菜单进入时的动画
     * @return
     */
    protected abstract int getInAnimation();
    /**
     * 菜单退出时的动画
     * @return
     */
    protected abstract int getOutAnimation();
}
